package com.zxy.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxy.util.Constant;
import com.zxy.mapper.ActivityMapper;
import com.zxy.mapper.ContractMapper;
import com.zxy.mapper.CourseMapper;
import com.zxy.pojo.entity.Course;
import com.zxy.pojo.entity.CourseContract;
import com.zxy.pojo.dto.ContractDTO;
import com.zxy.pojo.dto.CourseContractDTO;
import com.zxy.pojo.dto.PageDTO;
import com.zxy.pojo.entity.Activity;
import com.zxy.pojo.vo.CourseContractResponseVO;
import com.zxy.pojo.vo.CourseListVo;
import com.zxy.service.ContractService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, CourseContract> implements ContractService {
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private ActivityMapper activityMapper;

    //分页查询
    @Override
    public CourseContractResponseVO<CourseContractDTO> listContract(PageDTO pageDTO) {
        Page<CourseContract> pages = new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize());
        Page<CourseContractDTO> courseContractDTOS = contractMapper.listContract(pages, pageDTO);
        CourseContractResponseVO<CourseContractDTO> responseVO = new CourseContractResponseVO<>();
        responseVO.setTotal(pages.getTotal());
        responseVO.setRows(courseContractDTOS.getRecords());
        responseVO.setCode(Constant.SUCCESS);
        responseVO.setMsg(Constant.MSG);
        responseVO.setParams(null);
        return responseVO;
    }

    //条件查询课程和学科信息
    @Override
    public List<CourseListVo> listSelectCourse(String subject,Long courseId) {
       return contractMapper.listSelectCourse(subject,courseId);
    }

    //渠道来源
    @Override
    public List<CourseListVo> listSelectActivity(String channel) {
        return contractMapper.listSelectActivity(channel);
    }


    //添加合同  合同表添加数据
    @Override
    public void addContract(ContractDTO contractDTO) {
        CourseContract cos = new CourseContract();
        BeanUtils.copyProperties(contractDTO, cos);
        cos.setStatus(Constant.CONTRACTSTATUS);
        String filename = redisTemplate.opsForValue().get("filename");
        log.info("得到的上传文件名称是:{}",filename);
        cos.setFileName(filename);
        //课程原价去查询课程表
        Course course = courseMapper.selectById(contractDTO.getCourseId());
        cos.setCourseId(course.getId());
        cos.setCoursePrice(course.getPrice());
        //折扣类型去查询活动表
        Activity activity = activityMapper.selectById(contractDTO.getActivityId());
        cos.setDiscountType(activity.getName());
        //TODO
        //订单实付款金额 付款后修改
        cos.setOrderAmount(Constant.CONTRACTTYPE);
        //部门id 部门表未创建
        cos.setDeptId(Constant.CONTRACTTYPECODE);
        this.save(cos);
    }

    //根据id回显合同数据
    @Override
    public CourseContractDTO detail(Long id) {
        CourseContract courseContract = contractMapper.detail(id);
        CourseContractDTO courseContractDTO = new CourseContractDTO();
        BeanUtils.copyProperties(courseContract, courseContractDTO);
        courseContractDTO.setId(String.valueOf(courseContract.getId()));
        // Service 层：将 order_amount 赋值给 order
        courseContractDTO.setOrder(courseContract.getOrderAmount());
        return courseContractDTO;
    }


}
